from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import User

# 垃圾分类（如“湿垃圾”、“干垃圾”、“可回收物”等）
class Category(models.Model):
    name = models.CharField(max_length=50, verbose_name="分类名称", unique=True)
    description = models.TextField(blank=True, verbose_name="分类描述")
    icon = models.ImageField(upload_to='category_icons/', null=True, blank=True, verbose_name="图标")

    def __str__(self):
        return self.name

# 具体的垃圾条目（如“香蕉皮”、“旧电池”等）
class TrashItem(models.Model):
    name = models.CharField(max_length=100, verbose_name="垃圾名称", unique=True)
    alias = models.CharField(max_length=100, blank=True, null=True,verbose_name="别名/拼音/英文")
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name="所属分类")
    description = models.TextField(blank=True, null=True,verbose_name="垃圾说明")
    example_image = models.ImageField(upload_to='trash_images/', null=True, blank=True, verbose_name="示意图")

    def __str__(self):
        return self.name

# 用户查询历史（用于展示、统计）


class SearchHistory(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    keyword = models.CharField(max_length=100)
    searched_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f'{self.user.username} searched "{self.keyword}" at {self.searched_at}'

# 用户收藏夹（用于快速访问常用垃圾）
class Favorite(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    trash_item = models.ForeignKey(TrashItem, on_delete=models.CASCADE, verbose_name="收藏的垃圾")
    added_time = models.DateTimeField(auto_now_add=True, verbose_name="收藏时间")

    class Meta:
        unique_together = ('user', 'trash_item')

    def __str__(self):
        return f"{self.user.username} 收藏了 {self.trash_item.name}"

# 宣传文章 / 分类知识
class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    image = models.ImageField(upload_to='article_images/', null=True, blank=True)
    category = models.CharField(max_length=100, default='general')
    author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

# 用户反馈信息（选配）
class Feedback(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, verbose_name="用户")
    content = models.TextField(verbose_name="反馈内容")
    submitted_at = models.DateTimeField(auto_now_add=True, verbose_name="提交时间")
    is_resolved = models.BooleanField(default=False, verbose_name="是否已处理")

    def __str__(self):
        return f"来自{self.user.username if self.user else '匿名'}的反馈"



